/*
* @author : lj
* @creation : 2014-6-10 下午3:20:00
* @description : 
*	我的停车场管理，用户：停车场管理员，可以添加停车场和维护停车场信息
*/

package com.action.park;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.base.BaseAction;
import com.bean.ParkManager;
import com.service.ParkManagerService;
import com.tool.FileController;
import com.tool.FileController.FileUploadMode;
import com.util.EncoderHandler;
import com.util.GUtil;

public class PersonAction extends BaseAction {

	ParkManager manager;
	ParkManagerService service;
	List<Object> list=null;
	Integer flag;
	Timestamp ts = new Timestamp(System.currentTimeMillis());
	SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
	String date = sdf.format(ts);
	String pwd;
	
	private FileController fileController ;//头像上传过滤
 
	@Override
	public String handle() throws Exception {
		
		//头像上传过滤配置
		fileController = new FileController(
				FileController.IMAGE_EXTS, //运行上传的文件类型是图片
				FileController.FileUploadMode.ALLOW, //过滤模式 allow
				1024*500L); // max size 500KB
		fileController.setMsg("请上传图片文件，文件大小不大于500KB");
		session.put("fileController", fileController);
		
		if(id!=null && id>0)
			return this.lookview();
		return this.query();
	}
	@Override
	public String view() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
	//comment = (	ParkComment) service.get(id.intValue());
		if(manager==null)
			return GUtil.ERROR;
		return GUtil.VIEW;
	}
	public String lookview() throws Exception {
		try {
			parkuser=(ParkManager) session.get("parkuser");
			ParkManager p = (ParkManager) service.get(parkuser.getUserId());
			session.put("park", p);
			return GUtil.VIEW;
		} catch (Exception e) {
			e.getStackTrace();
			return GUtil.ERROR;
		}
	}
	@Override
	public String add() throws Exception {
		this.setHeaderTitle("添加");
		
		
		return GUtil.EDIT;
	}
	/*
	 * 修改基本信息
	 */
	public String updatePersionInfo() throws Exception {
		this.setHeaderTitle("修改基本信息");
		flag = 1;
		parkuser=(ParkManager) session.get("parkuser");
		if (id == null) {
			System.out.println("id不存在"+id);
			return GUtil.ERROR;
		}
		if (manager == null) {
			// beforeUpdate
			manager = (ParkManager) service.get(parkuser.getUserId());
			session.put("man", manager);
			if (manager == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			return GUtil.EDIT;
		}
		
		
		
		// update
		ParkManager p=(ParkManager) session.get("man");
		if(p==null){
			this.setLogmsg("信息不存在 id=" + id);
		}
		p.setIntroduction(manager.getIntroduction());
		p.setPhoto(manager.getPhoto());
		p.setRealName(manager.getRealName());
		p.setPhone(manager.getPhone());
		p.setUpdateTime(ts);
		
		if (service.update(p).endsWith(GUtil.SUCCESS)) {
			this.setMsg("修改成功");
			p = null;
			return this.query();
		} else {
			this.setMsg("修改失败");
			return GUtil.EDIT;
		}			
	}
	/*
	 * 修改密码
	 */
	public String modifyPassword() throws Exception {
		this.setHeaderTitle("修改密码");
		flag = 1;
		parkuser=(ParkManager) session.get("parkuser");
		if (id == null) {
			return GUtil.ERROR;
		}
		if (manager == null) {
			// beforeUpdate
			manager = (ParkManager) service.get(parkuser.getUserId());
			if (manager == null) {
				this.setLogmsg("信息不存在 id=" + id);
				return GUtil.ERROR;
			}
			session.put("mana", manager);
			
			return "mima";
		}
		// update
		
		ParkManager p=(ParkManager) session.get("mana");
		
		if(p==null){
			this.setLogmsg("信息不存在 id=" + id);
		}
		if(manager.getUserPasswd()!=null){
			if(!manager.getUserPasswd().trim().isEmpty()){
				p.setUserPasswd(EncoderHandler.EncoderPasswordBase64Md5(manager.getUserPasswd()));
			}
		}
		p.setUpdateTime(ts);
		if (service.update(p).endsWith(GUtil.SUCCESS)) {
			this.setMsg("修改密码成功");
			manager = null;
			return GUtil.LOGIN;
		} else {
			this.setMsg("修改密码失败");
			return "mima";
		}		
	}

	/**
	 * <p>Title: query 查询我的停车场信息</p>
	 * <p>Description: 查询我的停车场信息</p>
	 * @return 我的停车场列表
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		parkuser = (ParkManager) session.get("parkuser");
		if(parkuser==null)
			return GUtil.LOGIN;
		return lookview();
	}

	@Override
	public String delete() throws Exception {
		return this.query() ;//
	}
	/**
	 * @return the list
	 */
	public List<Object> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<Object> list) {
		this.list = list;
	}
	public ParkManager getManager() {
		return manager;
	}
	public void setManager(ParkManager manager) {
		this.manager = manager;
	}
	public ParkManagerService getService() {
		return service;
	}
	public void setService(ParkManagerService service) {
		this.service = service;
	}
	@Override
	public String update() throws Exception {
		return null;
	}
	public Integer getFlag() {
		return flag;
	}
	public void setFlag(Integer flag) {
		this.flag = flag;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}
	public void downExcel(){
		System.out.println("downloadExcel");
		Date date=new Date();
		DateFormat d=new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-Disposition","attachment;filename="+d.format(date)+".xls");
			// 注意这里的header参数为Content-Disposition而非第一种中的contentDisposition参数
						// Excel获得文件
//						// 打开一个文件的副本，并且指定数据写回到原文件
//						WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream());
//						// 添加一个工作表
//						WritableSheet sheet = book.createSheet("学生表", 0);
//						sheet.addCell(new Label(0, 0, "姓名"));
//						sheet.addCell(new Label(1, 0, "学号"));
//						sheet.addCell(new Label(2, 0, "专业"));
//						sheet.addCell(new Label(3, 0, "成绩"));
//						sheet.addCell(new Label(0, 1, "陈杰"));
//						sheet.addCell(new Label(1, 1, "s001"));
//						sheet.addCell(new Label(2, 1, "软件工程师"));
//						sheet.addCell(new jxl.write.Number(3, 1, 78));
//						book.write();
//						book.close();
						// 关闭输出流，同时也关闭response里的OutputStream，所以无需再调用response.getOutputStream().close()了
		} catch (Exception e) {
			e.getStackTrace();
		}
		
	}
}
